#include "CircleAddTool.h"

using namespace cmn;
using namespace input;
using namespace editor_core;

CircleAddTool::CircleAddTool( BodyEntity* parentBody ) :
	m_circle( Vec2::Zero(), 1.0f ),
	m_tempAction( 0 ),
	m_parentBody( parentBody )
{}

CircleAddTool::~CircleAddTool() {
	if( m_tempAction ) {
		m_tempAction->Undo();
		delete m_tempAction;
	}
}

IAction* CircleAddTool::Activate() { return 0; }
IAction* CircleAddTool::Deactivate() { return 0; }

IAction* CircleAddTool::ProcessMouseMove( MouseMoveEventArgs* mouseMoveEventArgs ) {
	Mouse mouse = mouseMoveEventArgs->GetMouseState();
	m_circle.Center = mouse.GetPosition();

	RefreshTempAction( m_circle, m_parentBody );
	return 0;
}

IAction* CircleAddTool::ProcessMouseButtonPress( MouseButtonEventArgs* mouseButtonEventArgs ) { return 0; }

IAction* CircleAddTool::ProcessMouseButtonRelease( MouseButtonEventArgs* mouseButtonEventArgs ) {
	if( mouseButtonEventArgs->GetButton() == MouseButton::Left ) {
		Mouse mouse = mouseButtonEventArgs->GetMouseState();
		m_circle.Center = mouse.GetPosition();

		RefreshTempAction( m_circle, m_parentBody );
		return new CircleCreateAction( m_circle, m_parentBody );
	}
	return 0;
}

IAction* CircleAddTool::ProcessMouseScroll( MouseScrollEventArgs* mouseScrollEventArgs ) {
	const float ratio = 0.1f;

	int delta = mouseScrollEventArgs->GetDelta();
	m_circle.Radius += ratio * delta;

	RefreshTempAction( m_circle, m_parentBody );
	return 0;
}

IAction* CircleAddTool::ProcessKeyPress( KeyEventArgs* keyEventArgs ) { return 0; }
IAction* CircleAddTool::ProcessKeyRelease( KeyEventArgs* keyEventArgs ) { return 0; }

void CircleAddTool::RefreshTempAction( Circle circle, BodyEntity* parentBody ) {
	if( m_tempAction != 0 ) {
		m_tempAction->Undo();
		delete m_tempAction;
		m_tempAction = 0;
	}

	m_tempAction = new CircleCreateAction( circle, parentBody );
	m_tempAction->Do();
}
